package com.symria.client.resource;

import java.io.File;
import java.util.List;

import net.sf.json.JSONObject;

import com.symria.framework.page.Page;
import com.symria.framework.page.PagerFactory;
import com.symria.framework.upload.util.UploadInfo;
import com.symria.framework.util.BaseAction;
import com.symria.framework.util.BeanToJson;
import com.symria.framework.util.Config;
import com.symria.framework.util.JsonUtil;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.resource.ResourceService;
import com.symria.server.po.AppResource;
import com.symria.system.constants.ATTResource;
import com.symria.system.constants.BaseAttribute;
import com.symria.system.util.SessionHelper;

/**
 * 用户资源管理模块
 * @author Administrator
 *
 */
public class ResourceAction extends BaseAction {

	private ResourceService resourceService;
	private File resource;
	private String resName;
	private String type;
	private String resourceFileName;
	private String path;
	private String message;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String listener(){
		JSONObject json = new JSONObject();
		UploadInfo uploadInfo = UploadMonitor.getUploadInfo();
		json.element("totalSize", uploadInfo.getTotalSize());
		json.element("readSize", uploadInfo.getBytesRead());
		//json.element("rate", Math.floor((uploadInfo.getBytesRead()*100)/uploadInfo.getTotalSize()));
		json.element("status", uploadInfo.getStatus());
		if("done".equals(uploadInfo.getStatus())){
			UploadMonitor.removeUploadInfo();
		}
		message=json.toString();
		return SUCCESS;
	}
	
	
	/**
	 * TODO:<B>功能描述:保存资源信息</B>
	 * @return
	 * Administrator--2012-10-31
	 */
	public String saveResource(){
		if(resource!=null){
			if(resourceService.checkSuffix(resourceFileName, Config.getValue("disabled.suffix"))){//过滤掉不让上传的文件
				addFieldError("uploadError", "您上传的文件为非法文件!");
				return ERROR;
			}else{
				String name=StringUtil.getFileNameNoSuffix(resourceFileName);
				AppResource r = resourceService.saveResource(resource, resourceFileName, name);
			    setPath(r.getResPath());
				setType(r.getResType());
			}		
		}
		return SUCCESS;
	}
    /**
     * TODO:<B>功能描述:查询资源列表ajax。</B>
     * @return
     * Administrator--2012-10-31
     */
    public String searchResource(){
    	Page page = PagerFactory.createPage();
    	resourceService.searchResource(SessionHelper.getSysUser().getId(), type,resName,page);
    	JSONObject obj = new JSONObject();
    	obj.element("list", BeanToJson.listTojson(page.getList()));
    	obj.element("foot", JsonUtil.getPageFoot(page));
    	setAjaxData(obj.toString());
    	return SUCCESS;
    }
    /**
     * TODO:<B>功能描述: 选择资源。</B>
     * @return
     * Administrator--2012-11-2
     */
    public String toResource(){
    	if(StringUtil.isEmpty(type)){this.setType(ATTResource.IMAGE.toString());}
    	setAttr(BaseAttribute.LIST, ATTResource.statusList);
    	return SUCCESS;
    }
    
	public ResourceService getResourceService() {
		return resourceService;
	}
	


	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}


	public File getResource() {
		return resource;
	}


	public void setResource(File resource) {
		this.resource = resource;
	}


	public String getResName() {
		return resName;
	}


	public void setResName(String resName) {
		this.resName = resName;
	}


	public String getResourceFileName() {
		return resourceFileName;
	}


	public void setResourceFileName(String resourceFileName) {
		this.resourceFileName = resourceFileName;
	}
	
	
}
